﻿using System;
using System.Text.RegularExpressions;
using System.Web;
namespace YanZhiwei.DotNet2.Utilities.AspNet
{
    /// <summary>
    ///URL 帮助类
    /// </summary>
    public static class UrlHelper
    {
        #region 添加URL参数
        /// <summary>
        /// 添加URL参数
        /// </summary>
        /// <param name="url">URL链接</param>
        /// <param name="paramName">参数名称</param>
        /// <param name="value">参数值</param>
        /// <returns>添加参数后URL</returns>
        public static string AddUrlParameters(this string url, string paramName, string value)
        {
            Uri _uri = new Uri(url);
            if (string.IsNullOrEmpty(_uri.Query))
            {
                string _urlEncode = HttpContext.Current.Server.UrlEncode(value);
                return String.Concat(url, "?" + paramName + "=" + _urlEncode);
            }
            else
            {
                string _urlEncode = HttpContext.Current.Server.UrlEncode(value);
                return String.Concat(url, "&" + paramName + "=" + _urlEncode);
            }
        }
        #endregion
        #region 更新URL参数
        /// <summary>
        /// 更新URL参数
        /// </summary>
        /// <param name="url">URL链接</param>
        /// <param name="paramName">参数名称</param>
        /// <param name="value">参数值</param>
        /// <returns>更新参数后URL</returns>
        public static string UpdateUrlParameters(this string url, string paramName, string value)
        {
            string _keyWord = paramName + "=";
            int _index = url.IndexOf(_keyWord) + _keyWord.Length;
            int _index1 = url.IndexOf("&", _index);
            if (_index1 == -1)
            {
                url = url.Remove(_index, url.Length - _index);
                url = string.Concat(url, value);
                return url;
            }
            url = url.Remove(_index, _index1 - _index);
            url = url.Insert(_index, value);
            return url;
        }
        #endregion
        #region 取得网站的根目录的URL
        /// <summary>
        /// 取得网站的根目录的URL
        /// </summary>
        /// <returns>网站的根目录的URL</returns>
        public static string GetRootURI()
        {
            string _appPath = "";
            HttpContext _httpCurrent = HttpContext.Current;
            HttpRequest _req;
            if (_httpCurrent != null)
            {
                _req = _httpCurrent.Request;
                string _urlAuthority = _req.Url.GetLeftPart(UriPartial.Authority);
                if (_req.ApplicationPath == null || _req.ApplicationPath == "/")
                    _appPath = _urlAuthority;
                else
                    _appPath = _urlAuthority + _req.ApplicationPath;
            }
            return _appPath;
        }
        /// <summary>
        /// 取得网站的根目录的URL
        /// </summary>
        /// <param name="reguest">HttpRequest</param>
        /// <returns>网站的根目录的URL</returns>
        public static string GetRootURI(HttpRequest reguest)
        {
            string _appPath = "";
            if (reguest != null)
            {
                string _urlAuthority = reguest.Url.GetLeftPart(UriPartial.Authority);
                if (reguest.ApplicationPath == null || reguest.ApplicationPath == "/")
                    _appPath = _urlAuthority;
                else
                    _appPath = _urlAuthority + reguest.ApplicationPath;
            }
            return _appPath;
        }
        #endregion
        #region 取得网站根目录的物理路径
        /// <summary>
        /// 取得网站根目录的物理路径
        /// </summary>
        /// <returns>网站根目录的物理路径</returns>
        public static string GetRootPath()
        {
            string _appPath = "";
            HttpContext _httpCurrent = HttpContext.Current;
            if (_httpCurrent != null)
            {
                _appPath = _httpCurrent.Server.MapPath("~");
            }
            else
            {
                _appPath = AppDomain.CurrentDomain.BaseDirectory;
                if (Regex.Match(_appPath, @"\\$", RegexOptions.Compiled).Success)
                    _appPath = _appPath.Substring(0, _appPath.Length - 1);
            }
            return _appPath;
        }
        #endregion 
    }
}